﻿using ProTrack.Entity;
using ProTrack.IRepository;
using ProTrack.IServices.Plans;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProTrack.Services.Plans
{
    public class TeamServices : ITeamServices
    {
        public TeamServices(IBaseRepository db)
        {
            Db = db;

        }

        public IBaseRepository Db { get; }

        public async Task<bool> AddAsync(Team model, List<TeamMember> list)
        {
            var result = await Db.InsertIdentityTranAsync(model, list);
            return result;
        }



        public async Task<bool> DeleteAsync(int[] Ids)
        {
            string sql = "delete TeamMember where TeamCode in (select TeamCode from Team where Id in @Ids); delete Team where Id in @Ids;";
            var result = await Db.DeleteAsync(sql, new { Ids });
            return result;
        }

        public async Task<bool> DeleteAsync(int Id)
        {
            string sql = "delete TeamMember where TeamCode = (select  TeamCode from Team where Id = @Id);  delete Team where Id = @Id;";
            var result = await Db.DeleteAsync(sql, new { Id });
            return result;
        }

        public async Task<IEnumerable<Team>> GetAllAsync()
        {
            string sql = "select * from Team";
            var list = await Db.QueryAllAsync<Team>(sql);
            return list;
        }

        public async Task<IEnumerable<Team>> GetAsync(int PageIndex, int PageSize, string TeamType, string TeamCode, string TeamName)
        {
            string sql = "select * from Team where 1=1 ";

            if (!string.IsNullOrEmpty(TeamType))
            {
                sql += " and TeamType like @TeamType ";
            }

            if (!string.IsNullOrEmpty(TeamCode))
            {
                sql += " and TeamCode like @TeamCode ";
            }


            if (!string.IsNullOrEmpty(TeamName))
            {
                sql += " and TeamName  = @TeamName ";
            }

            sql += " order by CreateTime desc offset @PageSize*(@PageIndex-1) row fetch next @PageSize rows only ";

            var list = await Db.QueryAsync<Team>(sql, new { PageIndex, PageSize, TeamType = $"%{TeamType}%", TeamCode = $"%{TeamCode}%", TeamName = $"%{TeamName}%" });
            return list;
        }

        public async Task<int> GetCountAsync(string TeamType, string TeamCode, string TeamName)
        {
            string sql = "select count(*) from Team where 1=1 ";
            if (!string.IsNullOrEmpty(TeamType))
            {
                sql += " and TeamType like @TeamType ";
            }

            if (!string.IsNullOrEmpty(TeamCode))
            {
                sql += " and TeamCode like @TeamCode ";
            }
            if (!string.IsNullOrEmpty(TeamName))
            {
                sql += " and TeamName  = @TeamName ";
            }


            var count = await Db.GetRecoreCountAsync(sql, new { TeamType = $"%{TeamType}%", TeamCode = $"%{TeamCode}%", TeamName = $"%{TeamName}%" });
            return count;
        }

        public async Task<IEnumerable<TeamMember>> GetTeamMemberbyCodeAsync(string TeamCode)
        {
            var list = await Db.QueryAsync<TeamMember>("select  * from TeamMember where TeamCode = @TeamCode", new { TeamCode });
            return list;
        }

        public async Task<IEnumerable<Employees>> GetTeamTypeAllAsync()
        {
            string sql = "select * from Employees where PersonCode not in (select  PersonCode from TeamMember)";
            var list = await Db.QueryAllAsync<Employees>(sql);
            return list;
        }



        public async Task<bool> UpdateAsync(Team model, List<TeamMember> list)
        {
            var result = await Db.UpdateIdentityTranAsync(model, list,"Id", "TeamCode","TeamCode");
            return result;
        }
    }
}
